package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.GoodsAlarmEnum;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsGoodsAlarmMapper;
import com.jy.wms.pojo.WmsGoodsAlarm;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
    * @ClassName: WmsGoodsAlarmService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Liyi
    * @Date Mon Dec 25 14:54:44 CST 2017
    */

    @Service
    public class WmsGoodsAlarmService extends BaseService<WmsGoodsAlarmMapper,WmsGoodsAlarm>{

    @BaseServiceDescribe(moduleName = "货品预警",btnName = "批量修改货品预警信息的预警状态")
    public void editYjzt (List<String> ids)throws ServiceException,SucessException {
        Integer count = dao.editYjzt(ids);
        if(count > 0){
            throw new SucessException("批量修改货品预警信息的预警状态成功");
        }else{
            throw new ServiceException("批量修改货品预警信息的预警状态失败");
        }
    }

    @BaseServiceDescribe(moduleName = "货品预警",btnName = "批量删除货品预警信息")
    public void del (List<String> ids)throws ServiceException,SucessException {
        Integer count = dao.del(ids);
        if(count > 0){
            throw new SucessException("批量删除货品预警信息成功");
        }else{
            throw new ServiceException("批量删除货品预警信息失败");
        }
    }

    /**
     * 根据仓库ID 获取当天预警信息
     * @param ckid
     * @return
     */
    @BaseServiceDescribe(moduleName = "货品预警",btnName = "货品预警列表")
    public List<WmsGoodsAlarm> getList(String ckid){
       return  dao.getList(ckid);
    }


    //private static Logger log = LoggerFactory.getLogger(WmsGoodsAlarmService.class);
    private SimpleDateFormat dateFormat() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 货品有效期预警定时任务
     * 生产日期 || 入库时间 + 有效期 - 失效期预警 >= 当前日期
     * @return int
     */
    @BaseServiceDescribe(moduleName = "预警定时任务",btnName = "货品有效期预警")
    @Transactional
    public Integer baseInsertQuartz() throws ServiceException {
        //查询所有需要有效期预警的货品信息
        List<WmsGoodsAlarm> alarmsList = new ArrayList<WmsGoodsAlarm>();
        Integer count = 0;
        try {
            List<Map<String,Object>> list = dao.selectAll();
            Date date = null;
            for (Map<String, Object> map : list) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //获取货品对应的 有效期周期类型 0生产日期 1入库日期
                String zqlx = map.get("zqlx").toString();
                if(zqlx.equals("0")){
                    //获取生产日期的Date类型
                    date = dateFormat.parse(map.get("scrq").toString());
                }else if(zqlx.equals("1")){
                    date = dateFormat.parse(map.get("rksj").toString());
                }
                Integer yxq = Integer.valueOf(map.get("yxq").toString());
                Integer sxqyj = Integer.valueOf(map.get("sxqyj").toString());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE,yxq-sxqyj);
                //如果计算的结果日期 >= 当前日期则加入预警
                if(calendar.getTime().getTime() >= System.currentTimeMillis()){
                    WmsGoodsAlarm wmsGoodsAlarm = new WmsGoodsAlarm();
                    wmsGoodsAlarm.setId(UUIDUtils.uuid());
                    wmsGoodsAlarm.setCkid(map.containsKey("ckid") ? map.get("ckid").toString() : "");
                    wmsGoodsAlarm.setSshzid(map.containsKey("sshzid") ? map.get("sshzid").toString():"");
                    //标题：预警类型+货品名称
                    wmsGoodsAlarm.setBt(GoodsAlarmEnum.yjlx_0.getType() + ":" + (map.containsKey("hpmc") ? map.get("hpmc").toString():""));
                    wmsGoodsAlarm.setXxxx("预警类型：" + GoodsAlarmEnum.yjlx_0.getType() +
                            "；货品编号：" + (map.containsKey("hpbh") ? map.get("hpbh").toString():"") +
                            "；货品名称：" + (map.containsKey("hpmc") ? map.get("hpmc").toString():"") +
                            "；批次：" + (map.containsKey("hppc") ? map.get("hppc").toString():"") +
                            "；生产日期：" + (map.containsKey("scrq") ? map.get("scrq").toString():"") +
                            "；库位名称：" + (map.containsKey("kwmc") ? map.get("kwmc").toString():"") +
                            "；托盘码：" + (map.containsKey("lpn") ? map.get("lpn").toString():"") +
                            "；供应商：" + (map.containsKey("gysmc") ? map.get("gysmc").toString():""));
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    wmsGoodsAlarm.setJyrq(sdf.format(new Date()));
                    wmsGoodsAlarm.setYjzt("0");
                    wmsGoodsAlarm.setYjlx("0");
                    alarmsList.add(wmsGoodsAlarm);
                }
            }
            if(alarmsList.size()>0){
                count = dao.baseInsertAll(alarmsList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("货品有效期预警异常",e);
        }
        return count;
    }

    /**
     * 货品有效期预警定时任务
     * 生产日期 || 入库时间 + 有效期 - 失效期预警 >= 当前日期
     * @return int
     */
    @BaseServiceDescribe(moduleName = "预警定时任务",btnName = "货品库存预警")
    @Transactional
    public Integer baseInsertGoodsQuartz() throws ServiceException {
        //查询所有需要有效期预警的货品信息
        List<WmsGoodsAlarm> alarmsList = new ArrayList<WmsGoodsAlarm>();
        Integer count = 0;
        try {
            List<Map<String,Object>> list = dao.selectGoods();
            Date date = null;
            for (Map<String, Object> map : list) {
                Integer sl = map.containsKey("sl") ? Integer.valueOf(map.get("sl").toString()) : 0;
                Integer kczdl = map.containsKey("kczdl") ? Integer.valueOf(map.get("kczdl").toString()) : 0;
                Integer kczxl = map.containsKey("kczxl") ? Integer.valueOf(map.get("kczxl").toString()) : 0;
                if(sl >= kczdl || sl <= kczxl){
                    WmsGoodsAlarm wmsGoodsAlarm = new WmsGoodsAlarm();
                    wmsGoodsAlarm.setId(UUIDUtils.uuid());
                    wmsGoodsAlarm.setCkid(map.containsKey("ckid") ? map.get("ckid").toString() : "");
                    wmsGoodsAlarm.setSshzid(map.containsKey("sshzid") ? map.get("sshzid").toString():"");
                    if(sl >= kczdl){
                        //标题：预警类型+货品名称
                        wmsGoodsAlarm.setBt(GoodsAlarmEnum.yjlx_2.getType() + ":" + (map.containsKey("hpmc") ? map.get("hpmc").toString():""));
                        wmsGoodsAlarm.setXxxx("预警类型：" + GoodsAlarmEnum.yjlx_2.getType() +
                                "；货品编号：" + (map.containsKey("hpbh") ? map.get("hpbh").toString():"") +
                                "；货品名称：" + (map.containsKey("hpmc") ? map.get("hpmc").toString():"") +
                                "；数量：" + (map.containsKey("sl") ? map.get("sl").toString():"")+
                                "；库存最大量：" + (map.containsKey("kczdl") ? map.get("kczdl").toString():"") +
                                "；库存最小量：" + (map.containsKey("kczxl") ? map.get("kczxl").toString():""));
                        wmsGoodsAlarm.setYjlx("2");
                    } else if(sl <= kczxl){
                        wmsGoodsAlarm.setBt(GoodsAlarmEnum.yjlx_1.getType() + (map.containsKey("hpmc") ? map.get("hpmc").toString():""));
                        wmsGoodsAlarm.setXxxx("预警类型：" + GoodsAlarmEnum.yjlx_1.getType() +
                                "；货品编号：" + (map.containsKey("hpbh") ? map.get("hpbh").toString():"") +
                                "；货品名称：" + (map.containsKey("hpmc") ? map.get("hpmc").toString():"") +
                                "；数量：" + (map.containsKey("sl") ? map.get("sl").toString():"")+
                                "；库存最大量：" + (map.containsKey("kczdl") ? map.get("kczdl").toString():"") +
                                "；库存最小量：" + (map.containsKey("kczxl") ? map.get("kczxl").toString():""));
                        wmsGoodsAlarm.setYjlx("1");
                    }
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    wmsGoodsAlarm.setJyrq(sdf.format(new Date()));
                    wmsGoodsAlarm.setYjzt("0");
                    alarmsList.add(wmsGoodsAlarm);
                }
            }
            if(alarmsList.size()>0){
                count = dao.baseInsertAll(alarmsList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("货品有效期预警异常",e);
        }
        return count;
    }

     /**
     * 插入一条记录
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert( WmsGoodsAlarm entity){
         return dao.baseInsert( entity );
    }

   /**
    * 插入一条记录（匹配有值的字段）
    * @param entity 实体对象
    * @return int
    */
    public Integer baseInsertSelective( WmsGoodsAlarm entity){
         return dao.baseInsertSelective( entity);
    }

   /**
    * 删除（根据主键ID删除）
    * @param id 主键
    * @return int
    */
    public Integer baseDeleteByPrimaryKey( String id){
         return dao.baseDeleteByPrimaryKey( id);
    }

   /**
    * 删除（根据ID 批量删除）
    * @param idList 主键ID列表
    * @return int
    */
    public Integer baseDeleteBathPrimaryKeys( List<String> idList){
         return dao.baseDeleteBathPrimaryKeys( idList );
    }

    /**
    * 分页查询
    * @param entity 实体
    * @return Page<WmsGoodsAlarm>
    */
    public Page<WmsGoodsAlarm> baseSelectPage(WmsGoodsAlarm entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        return (Page<WmsGoodsAlarm>)dao.baseSelectPage( entity );
    }

   /**
    * 查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsGoodsAlarm
    */
    public WmsGoodsAlarm baseSelectByPrimaryKey( String id ){
         return dao.baseSelectByPrimaryKey( id );
    }

   /**
    * 查询（根据ID 批量查询）
    * @param idList 主键ID列表
    * @return list
    */
    public Integer baseSelectBathPrimaryKeys( List<WmsGoodsAlarm> idList ){
         return dao.baseSelectBathPrimaryKeys( idList );
    }

   /**
    *修改 （匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKeySelective( WmsGoodsAlarm entity){
         return dao.baseUpdateByPrimaryKeySelective( entity);
    }

   /**
    *修改（根据主键ID修改）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKey( WmsGoodsAlarm entity){
         return dao.baseUpdateByPrimaryKey( entity);
    }
}
